package cn.devzyh.service.impl;

import cn.devzyh.dao.CourseMapper;
import cn.devzyh.domain.Course;
import cn.devzyh.domain.CourseVo;
import cn.devzyh.domain.Teacher;
import cn.devzyh.service.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Course> findByCondition(CourseVo courseVo) {

        return courseMapper.findByCondition(courseVo);
    }

    @Override
    @Transactional
    public void saveCourseAndTeacher(CourseVo courseVo) {

        Date now = new Date();

        // 复制课程对象信息
        Course course = new Course();
        BeanUtils.copyProperties(courseVo, course);
        course.setCreateTime(now);
        course.setUpdateTime(now);
        courseMapper.saveCourse(course);

        // 复制教师对象信息
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(courseVo, teacher);
        teacher.setCourseId(course.getId());
        teacher.setCreateTime(now);
        teacher.setUpdateTime(now);
        courseMapper.saveTeacher(teacher);
    }

    @Override
    @Transactional
    public void updateCourseAndTeacher(CourseVo courseVo) {

        Date now = new Date();

        // 复制课程对象信息
        Course course = new Course();
        BeanUtils.copyProperties(courseVo, course);
        course.setUpdateTime(now);
        courseMapper.updateCourse(course);

        // 复制教师对象信息
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(courseVo, teacher);
        teacher.setCourseId(course.getId());
        teacher.setUpdateTime(now);
        courseMapper.updateTeacher(teacher);
    }

    @Override
    public Course findOneById(Integer id) {

        return courseMapper.findOneById(id);
    }

    @Override
    public void updateCourseStatus(Integer id, Integer status) {

        Course course = new Course();
        course.setId(id);
        course.setStatus(status);
        course.setUpdateTime(new Date());
        courseMapper.updateCourseStatus(course);
    }
}
